package com.chenjun.web.controller.sys;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.Constant;
import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.core.dao.generator.entity.SysDictionary;
import com.chenjun.core.service.sys.SysDictionaryService;
import com.chenjun.core.vo.sys.SysDictionaryDto;
import com.chenjun.web.controller.BaseController;

/**
 * 
 * @Description: 字典管理
 * @author yanjinyin
 * @date 2017年7月29日 下午2:35:34
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("dictionary")
public class SysDictionaryController extends BaseController {

	@Autowired
	private SysDictionaryService sysDictionaryService;

	/**
	 * 页面展示
	 * @return
	 * ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("dictionary/list");
		final Map<String,String> param = new HashMap<String,String>(2);
		param.put("key",Constant.Dictionary.SYS_TYPE.getValue());
		param.put("value", Constant.Dictionary.SYS_TYPE.getName());
		
		final Map<String,String> paramClassify = new HashMap<String,String>(2);
		paramClassify.put("key",Constant.Dictionary.CLASSIFY_TYPE.getValue());
		paramClassify.put("value", Constant.Dictionary.CLASSIFY_TYPE.getName());
		
		final Map<String,String> paramUseplace = new HashMap<String,String>(2);
		paramUseplace.put("key",Constant.Dictionary.USEPLACE_TYPE.getValue());
		paramUseplace.put("value", Constant.Dictionary.USEPLACE_TYPE.getName());
		
		final Map<String,String> paramInsure = new HashMap<String,String>(2);
		paramInsure.put("key",Constant.Dictionary.INSURE_TYPE.getValue());
		paramInsure.put("value", Constant.Dictionary.INSURE_TYPE.getName());
		
		List<Map<String,String>> list = new ArrayList<Map<String,String>>(1) {{
		    add(param);
		    add(paramClassify);
		    add(paramUseplace);
		    add(paramInsure);
		}};
		mv.addObject("dictionarys",list);
		return mv;
	}
	
	/**
	 * 分页搜索
	 * @param datagrid
	 * @return
	 * ResultDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		datagrid.getSortBy().add(new DataGridSortModel("created_time", DataGridSortModel.DESC));
		Pagination data = sysDictionaryService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	/**
	 * 编辑页面元素
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(@Valid SysDictionaryDto dto,Errors errors)  throws IllegalAccessException, InvocationTargetException{
		ResultDto rtDto = new ResultDto();
		
		//验证
		if (!hanleErrors(rtDto, errors)) { return rtDto; }
		
		DataGridModel dgm = new DataGridModel();
		dgm.getParams().put("name", dto.getName());
		dgm.getParams().put("type", dto.getType());
		dgm.getParams().put("nId", dto.getId());
		List<SysDictionary> dicts = sysDictionaryService.doListByCondition(dgm);
		if(!CollectionUtils.isEmpty(dicts)){
			rtDto.resultWaring();
			rtDto.setMessage("该字典名称已经存在");
			return rtDto;
		}
		
		int i = sysDictionaryService.createOrUpdate(SysDictionaryDto.getModelByDto(dto));
		if(i<=0){
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		SysDictionary sysDictionary = sysDictionaryService.get(id);
		int result = 0;
		if (sysDictionary != null) {
			result = sysDictionaryService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(sysDictionaryService.get(id));
		return rtDto;
	}

}
